using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows;

namespace Ricciolo.PaperBoy.UI.Converters
{
    public class NumberToVisibilityConverter : IValueConverter
    {
        private Operators _operator = Operators.Equal;

        public Operators Operator
        {
            get { return _operator; }
            set { _operator = value; }
        }

        #region IValueConverter Members

        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (parameter == null)
                parameter = 0;
            if (value == null)
                value = 0;

            // Convert reference parameter to the same type
            parameter = Convert.ChangeType(parameter, value.GetType());

            bool r = false;
            if (this.Operator == Operators.Equal)
                r = (value.Equals(parameter));
            else if (this.Operator == Operators.NotEqual)
                r = !(value.Equals(parameter));
            else if (this.Operator == Operators.MajorThan)
            {
                if (value is Int16)
                    r = ((Int16)value > (Int16)parameter);
                if (value is Int32)
                    r = ((Int32)value > (Int32)parameter);
                if (value is Int64)
                    r = ((Int64)value > (Int64)parameter);
                if (value is Double)
                    r = ((Double)value > (Double)parameter);
            }
            else if (this.Operator == Operators.MinorThan)
            {
                if (value is Int16)
                    r = ((Int16)value < (Int16)parameter);
                if (value is Int32)
                    r = ((Int32)value < (Int32)parameter);
                if (value is Int64)
                    r = ((Int64)value < (Int64)parameter);
                if (value is Double)
                    r = ((Double)value < (Double)parameter);
            }

            return (r) ? Visibility.Visible : Visibility.Collapsed;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

    public enum Operators
    {
        Equal,
        NotEqual,
        MajorThan,
        MinorThan
    }
}
